package com.bringway.global.util;
import java.security.MessageDigest;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.bringway.global.domain.SysUserDetails;

/*
* @author qjy
* @version 创建时间：2015年9月14日 下午4:23:11
* 类说明
*/
public class UserUtil {

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
			"e", "f" };

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	// 返回形式为数字跟字符串
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		// System.out.println("iRet="+iRet);
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}
	
	public static String getMD5Password(String password) throws Exception{
		MessageDigest md = MessageDigest.getInstance("MD5");
        // md.digest() 该函数返回值为存放哈希值结果的byte数组
        String result = byteToString(md.digest(password.getBytes()));
        return result;
	}
	
	/**
     * 获取用户信息
     * @author mengbin
     * @date 2014年2月26日 下午6:57:41
     */
    public static SysUserDetails getCurrentUser()
    {
    	SecurityContext sc = SecurityContextHolder.getContext();
    	if(sc.getAuthentication() == null)
    	{
    		return null;
    	}
    	Object obj = sc.getAuthentication().getPrincipal();
    	if((obj == null) || (!(obj instanceof UserDetails)))
    	{
    		return null;
    	}
    	return (SysUserDetails)obj;
    }
    
    public static String getCurrentUserId(){
    	SysUserDetails ud = UserUtil.getCurrentUser();
    	if(null != ud){
    		return ud.getGyh();
    	}else{
    		return "";
    	}
    }
    
    public static String getCurrentUsername(){
    	SysUserDetails ud = UserUtil.getCurrentUser();
    	if(null != ud){
    		return ud.getUsername();
    	}else{
    		return "";
    	}
    }
    
    /**
     * 获取登录IP地址
     * @author mengbin
     * @date 2014年2月26日 下午7:00:40
     */
    public static String getRemoteAddr(HttpServletRequest request)
	{
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
		}
		return ip;
	}

    /**
     * 获取上一跳转路径
     * @author mengbin
     * @date 2014年3月3日 下午6:51:07
     */
    public static String getReferUrl(HttpServletRequest request)
    {
    	return request.getHeader("Referer");
    }

}
